import { createStore } from "vuex"; 
import axios from "axios";
//创建store实例
const store=createStore({  
       //通过store.commit方法来触发状态变更
       state(){ //是存储的单一状态，是存儲的基本数据
        return{
            List:[],
            songid:'',
            picUrl:"",
            songname:"",
            singer:"",
            lrc:'',
            
         }
       },
       getters:{//store里的计算属性
        
       },
       mutations:{    //通过store.commit方法触发对应函数状态变更， mutation是同步函数
                changeList(state,value){
                  state.List=value;
                },
                changefootmsg(state,value){
                       state.picUrl=value.value1;
                       state.songname=value.value2;
                       state.singer=value.value3;
                },
                getcurrentLrc(state,value){
                  state.lrc=value; 
                },
                getSongUrl(state,value){
                  state.songUrl=value;
                }
       },
    actions:{
      search(context,value){
        axios.post("http://localhost:3000/search?keywords="+value).then((res)=>{  //搜索
          // console.log(res.data);
          context.commit("changeList",res.data.result.songs);
        })
      
      },
      getcurrentplay(context,value){
        axios.post("http://localhost:3000/song/detail?ids="+value).then((res)=>{ //获取歌曲详细信息
          // console.log(res);
          context.commit("changefootmsg",{
            value1:res.data.songs[0].al.picUrl,
            value2:res.data.songs[0].name,
            value3:res.data.songs[0].ar[0].name});
        })
      },
      getlyc(context,value){
        axios.post("http://localhost:3000/lyric?id="+value).then((res)=>{
          console.log(res.data.lrc.lyric);
          context.commit("getcurrentLrc",res.data.lrc.lyric)
        })
     },
    }
})

export default store

